package com.ble;


import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.util.Log;

@SuppressLint("NewApi")
public class BlueToothConfig {
	
	private static final String TAG = "BluetoothConfig"; 
	
	private BluetoothAdapter mBluetoothAdapter = null;
	private BluetoothManager mBluetoothManager = null;

	private ICallBack callBack;
	
	public   interface  ICallBack {   
	    void  execute(final BluetoothDevice device, final int rssi, final byte[] scanRecord);   
	}   
	
	public void setCallBack(ICallBack callBack) {  
	   this.callBack =callBack;  
	}   
	   
    public void activeCallBack(final BluetoothDevice device, final int rssi, final byte[] scanRecord){   
    	callBack.execute(device,rssi,scanRecord);///进行回调操作       
    }   
	
	Context mContext;
 	public BlueToothConfig(Context mctr){
 		mContext = mctr;
 		mBluetoothManager = (BluetoothManager)mContext.getSystemService(Context.BLUETOOTH_SERVICE);
 		if (!mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
 		   Log.d(TAG, "device is not support ble");
 		   return;
 		}

        mBluetoothAdapter = mBluetoothManager.getAdapter();
	}
 	
 	

 	public void openBle(){
        if (!mBluetoothAdapter.isEnabled()) {
            if (!mBluetoothAdapter.isEnabled()) {
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                mBluetoothAdapter.enable();
            }
        } 
 	}
 	 
// 	Handler handler = new Handler();
 	private BluetoothAdapter.LeScanCallback mBleScanCallback =
            new BluetoothAdapter.LeScanCallback(){

			@Override
			public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord){
				// TODO Auto-generated method stub
				activeCallBack(device,rssi,scanRecord);
			}
 		
 	};
 	 
 	public void stopBleScan(){
 		mBluetoothAdapter.stopLeScan(mBleScanCallback);
 	}
 	
 	public Boolean checkBleOpen(){ 
        if (mBluetoothAdapter.isEnabled()) {
        	return true;
        }
 		return false;
 	}
 	 
 	public void startBleScan(){
 		if(mBluetoothAdapter.isDiscovering())
 			Log.d("test","blestart");
 		else 
			Log.d("test","blestop");
		
 		Log.d(TAG,"START TEST");
//		handler.postDelayed(new Runnable() {
//            @Override 
//            public void run() {
//         		if(mBluetoothAdapter.isDiscovering())
//         			Log.d("test","blestart");
//         		else 
//        			Log.d("test","blestop");
//            	mBluetoothAdapter.stopLeScan(mBleScanCallback);
//            }
//        }, 10000);
 		
 		mBluetoothAdapter.startLeScan(mBleScanCallback);
 	}
 	  
 	
    public static String bytesToString(byte[] bytes, int length)
    {
    	StringBuffer buf = new StringBuffer();
    	for(int i=0 ;i<length ;i++) 
    	{
    		buf.append(byteToString(bytes[i]));
    	} 
    	return buf.toString();
    }
  
    public static String byteToString(byte b) {   
    	byte high, low;   
    	byte maskHigh = (byte)0xf0;   
    	byte maskLow = 0x0f;   
    	  
    	high = (byte)((b & maskHigh) >> 4);   
    	low = (byte)(b & maskLow);
    	  
    	StringBuffer buf = new StringBuffer();   
    	buf.append(findHex(high));    
    	buf.append(findHex(low));   
    	  
    	return buf.toString();   
    }   
    	  
    private static char findHex(byte b) {   
    	int t = new Byte(b).intValue();   
    	t = t < 0 ? t + 16 : t;   
    	  
    	if ((0 <= t) &&(t <= 9)) {   
    	return (char)(t + '0');   
    	}   
    	  
    	return (char)(t-10+'A');   
    } 
}
